class MainController < ApplicationController

  def index
  end

  def register
    if request.post?
      @user = User.new params[:user]
      if @user.save
        flash[:info] = 'Registration complete!'
        redirect_to :action => 'index', :controller => 'main'
      end
    end
  end
  
  def login
    if request.post?
      @user = User.find_by_username(params[:username])
	if @user and @user.password_is? params[:password]
	  session[:uid] = @user.id
	  if params[:remember] # if user wants to be remembered
            cookie_pass = [Array.new(9){rand(256).chr}.join].pack("m").chomp
            cookie_hash = Digest::MD5.hexdigest(cookie_pass + @user.password_salt)
            cookies[:roomies_login_pass] = { :value => cookie_pass, :expires => 30.days.from_now }
            cookies[:roomies_username] = { :value => @user.username, :expires => 30.days.from_now }
	    User.update(@user.id, :cookie_hash => cookie_hash)
          end
          redirect_to :controller => 'home'
	else 
	   @auth_error = 'Wrong username or password.'
	end
    end
  end

  def view
    @user = User.find(session[:uid])
    @relationship = Relationship.find(params[:id])
  end

end
